package com.hz.monitoring.module.hardware.controller.admin.mcu_sys_type;

import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.excel.core.util.ExcelUtils;
import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import com.hz.monitoring.module.hardware.controller.admin.mcu_sys_type.vo.*;
import com.hz.monitoring.module.hardware.convert.mcu_sys_type.McuSysTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.mcu_sys_type.McuSysTypeDO;
import com.hz.monitoring.module.hardware.service.mcu_sys_type.McuSysTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - MCU系统分类")
@RestController
@RequestMapping("/hardware/mcu-sys-type")
@Validated
public class McuSysTypeController {

    @Resource
    private McuSysTypeService mcuSysTypeService;

    @PostMapping("/create")
    @ApiOperation("创建MCU系统分类")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:create')")
    public CommonResult<Long> createMcuSysType(@Valid @RequestBody McuSysTypeCreateReqVO createReqVO) {
        return success(mcuSysTypeService.createMcuSysType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新MCU系统分类")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:update')")
    public CommonResult<Boolean> updateMcuSysType(@Valid @RequestBody McuSysTypeUpdateReqVO updateReqVO) {
        mcuSysTypeService.updateMcuSysType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除MCU系统分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:delete')")
    public CommonResult<Boolean> deleteMcuSysType(@RequestParam("id") Long id) {
        mcuSysTypeService.deleteMcuSysType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得MCU系统分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:query')")
    public CommonResult<McuSysTypeRespVO> getMcuSysType(@RequestParam("id") Long id) {
        McuSysTypeDO mcuSysType = mcuSysTypeService.getMcuSysType(id);
        return success(McuSysTypeConvert.INSTANCE.convert(mcuSysType));
    }

    @GetMapping("/list")
    @ApiOperation("获得MCU系统分类列表")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:query')")
    public CommonResult<List<McuSysTypeRespVO>> getMcuSysTypeList(McuSysTypeListReqVO listVO) {
        List<McuSysTypeDO> list = mcuSysTypeService.getMcuSysTypeList(listVO);
        return success(McuSysTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得MCU系统分类分页")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:query')")
    public CommonResult<PageResult<McuSysTypeRespVO>> getMcuSysTypePage(@Valid McuSysTypePageReqVO pageVO) {
        PageResult<McuSysTypeDO> pageResult = mcuSysTypeService.getMcuSysTypePage(pageVO);
        return success(McuSysTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出MCU系统分类 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-sys-type:export')")
    @OperateLog(type = EXPORT)
    public void exportMcuSysTypeExcel(@Valid McuSysTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<McuSysTypeDO> list = mcuSysTypeService.getMcuSysTypeList(exportReqVO);
        // 导出 Excel
        List<McuSysTypeExcelVO> datas = McuSysTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "MCU系统分类.xls", "数据", McuSysTypeExcelVO.class, datas);
    }

}
